Imports System.Windows.Forms
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree

Namespace Pdf
	Public Partial Class ActionEditorForm
		Inherits Form

		#Region "Fields"

		Private _document As PdfDocument

		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			positionComboBox.Items.Add(PdfDestinationType.Fit)
			positionComboBox.Items.Add(PdfDestinationType.FitHorizontal)
			positionComboBox.Items.Add(PdfDestinationType.FitVertical)
		End Sub

		Public Sub New(document As PdfDocument, action As PdfAction)
			Me.New()
			_document = document
			_action = action
			pageNumberNumericUpDown.Maximum = document.Pages.Count
			tabControl1.SelectedTab = noActionTabPage
			positionComboBox.SelectedItem = PdfDestinationType.Fit
			Dim documentContainsPages As Boolean = document.Pages.Count > 0
			If Not documentContainsPages Then
				tabControl1.TabPages.Remove(gotoActionTabPage)
			End If
			If _action IsNot Nothing Then
				Select Case _action.ActionType
					Case PdfActionType.[GoTo]
						If documentContainsPages Then
							Dim dest As PdfDestinationBase = DirectCast(_action, PdfGotoAction).Destination
							If dest IsNot Nothing AndAlso dest.Page IsNot Nothing Then
								pageNumberNumericUpDown.Value = document.Pages.IndexOf(dest.Page) + 1
								If positionComboBox.Items.Contains(dest.DestinationType) Then
									positionComboBox.SelectedItem = dest.DestinationType
								Else
									positionComboBox.SelectedItem = PdfDestinationType.Fit
								End If
							End If
							tabControl1.SelectedTab = gotoActionTabPage
						End If
						Exit Select
					Case PdfActionType.Launch
						commandLineTextBox.Text = DirectCast(_action, PdfLaunchAction).WinCommandLine
						tabControl1.SelectedTab = launchActionTabPage
						Exit Select
					Case PdfActionType.URI
						uriTextBox.Text = DirectCast(_action, PdfUriAction).URI
						tabControl1.SelectedTab = uriActionTabPage
						Exit Select
				End Select
			End If
		End Sub

		#End Region



		#Region "Properties"

		Private _action As PdfAction
		Public ReadOnly Property Action() As PdfAction
			Get
				Return _action
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub buttonOK_Click(sender As Object, e As EventArgs)
			Select Case tabControl1.SelectedIndex
				Case 0
					_action = Nothing
					Exit Select
				Case 1
					If _action Is Nothing OrElse Not (TypeOf _action Is PdfGotoAction) Then
						_action = New PdfGotoAction(_document, CreateDestination())
					Else
						Dim dest As PdfDestinationBase = DirectCast(_action, PdfGotoAction).Destination
						If dest.DestinationType <> DirectCast(positionComboBox.SelectedItem, PdfDestinationType) Then
							dest = CreateDestination()
						End If
						DirectCast(_action, PdfGotoAction).Destination = dest
					End If
					Exit Select
				Case 2
					If _action Is Nothing OrElse Not (TypeOf _action Is PdfLaunchAction) Then
						_action = New PdfLaunchAction(_document, commandLineTextBox.Text)
					Else
						DirectCast(_action, PdfLaunchAction).WinCommandLine = commandLineTextBox.Text
					End If
					Exit Select
				Case 3
					If _action Is Nothing OrElse Not (TypeOf _action Is PdfUriAction) Then
						_action = New PdfUriAction(_document, uriTextBox.Text)
					Else
						DirectCast(_action, PdfUriAction).URI = uriTextBox.Text
					End If
					Exit Select
			End Select
			DialogResult = DialogResult.OK
		End Sub

		Private Function CreateDestination() As PdfDestinationBase
			Dim page As PdfPage = _document.Pages(CInt(Math.Truncate(pageNumberNumericUpDown.Value - 1)))
			Select Case DirectCast(positionComboBox.SelectedItem, PdfDestinationType)
				Case PdfDestinationType.Fit
					Return New PdfDestinationFit(_document, page)
				Case PdfDestinationType.FitHorizontal
					Return New PdfDestinationFitHorizontal(_document, page)
				Case PdfDestinationType.FitVertical
					Return New PdfDestinationFitVertical(_document, page)
			End Select
			Return Nothing

		End Function

		#End Region

	End Class
End Namespace
